/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Graficos;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Linton
 */
public class GraficoHistograma {


    public static ChartPanel gerarGraficoHistograma(int[] dados) {

        IntervalXYDataset dataset = createDataset(dados);

        JFreeChart chart = createChart(dataset);

        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

        return chartPanel;

    }

    private static IntervalXYDataset createDataset(int[] data) {
        final XYSeries series = new XYSeries("frequencia");

        for (int i = 0; i < data.length; i++) {
            series.add(i, data[i]); //int j = data[i];

        }
        final XYSeriesCollection dataset = new XYSeriesCollection(series);
        return dataset;
    }

    private static JFreeChart createChart(IntervalXYDataset dataset) {
        final JFreeChart chart = ChartFactory.createXYBarChart(
                "Histograma",
                "Nivel de cinza",
                false,
                "Numero de ocorrencias",
                dataset,
                PlotOrientation.VERTICAL,
                false,
                true,
                false);
        return chart;
    }
}
